package com.example.demo.controller.surperController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.MedicalMapper;
import com.example.demo.Mapper.SpeechMapper;
import com.example.demo.base.Result;
import com.example.demo.entity.Medical;
import com.example.demo.entity.Speech;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/dataSurper")
public class DataSurperController {

    @Resource
    SpeechMapper speechMapper;

    @Resource
    MedicalMapper medicalMapper;

    //13-14讲话-获取数据库数据
    @GetMapping(value = "/speech")
    public Result<?> findPageS(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<Speech> wrapper = Wrappers.<Speech>lambdaQuery();
        Page<Speech> speechPage = speechMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(speechPage);
    }

    @PostMapping(value = "/speech")
    public Result<?> saveS(@RequestBody String Text) {
        Speech speech = new Speech();
        if (Text.equals("")) {
            return Result.error(0, "文本不能为空");
        }
        speech.setText(Text);
        speechMapper.insert(speech);
        return Result.success();
    }

    //    @PutMapping(value = "/speech")
//    public Result<?> updateS(@RequestBody Speech speech) {
//        speechMapper.updateById(speech);
//        return Result.success();
//    }
//
    @DeleteMapping("/speech/{id}")
    public Result<?> deleteS(@PathVariable Integer id) {
        speechMapper.deleteById(id);
        return Result.success();
    }

    //医疗数据
    @GetMapping(value = "/medical")
    public Result<?> findPageM(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<Medical> wrapper = Wrappers.<Medical>lambdaQuery();
        Page<Medical> medicalPage = medicalMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(medicalPage);
    }

    @PostMapping(value = "/medical")
    public Result<?> saveM(@RequestBody String Text) {
        Medical medical = new Medical();
        if (Text.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        medical.setText(Text);
        medicalMapper.insert(medical);
        return Result.success();
    }

    //    @PutMapping(value = "/medical")
//    public Result<?> updateM(@RequestBody Medical medical) {
//        medicalMapper.updateById(medical);
//        return Result.success();
//    }
//
    @DeleteMapping("/medical/{id}")
    public Result<?> deleteM(@PathVariable Integer id) {
        medicalMapper.deleteById(id);
        return Result.success();
    }

}
